'''
Author: @zhyy
Date: 2021-01-27 14:23:32
LastEditors: @zhyy
LastEditTime: 2021-02-07 17:51:36
'''
from app.models.model import Article, ArticleSort, ArticleComment, ReplyComment
from app.models.model_schema import ArticleSortchema
from app.func.query_to_json import to_one_json,to_many_json
from app.cache.redis_cache import db_cache
from app.func.time_converter import get_now_time
from app import db

def cache_arc_sort(page,per_page,index_name,data_name,k,s):
    try:
        arc_sorts = db.session.query(ArticleSort).filter(ArticleSort.delete_time==None).paginate(
                page, per_page, error_out=False)
    # 数据库读取异常
    except Exception as e:
        return {"status":1,"msg":"数据库读取异常"}
    # 数据读取空数据
    if  not arc_sorts:
        return {"status":1,"msg":"读取数据库文件为空"}
        # 成功读取到数据
        # print(arc_sorts)
    arc_sort_chem = db_cache.to_many_json(data=arc_sorts.items,chema=ArticleSortchema)
    if arc_sorts.pages< page:
        return {"status":1,"msg":"缓存数据完成"}
    # print("数据库读取：",arc_sort_data)
    # 把成功读取的数据写入redis缓存
    try:
        arc_sort_data_cache=db_cache.set_cache(arc_sort_chem,arc_sorts.total,index_name,data_name,k,s)
    except Exception as e:
        return {"status":1,"msg":"缓存数据异常"}
    return {"status":0,"msg":f"缓存第{page}页数据成功"}
    


